package com.boot2.core.web;

import com.boot2.core.lifecycle.SmartLifecycleHandler;
import com.boot2.core.model.SysLog;
import com.boot2.core.service.SysLogService;
import com.boot2.core.utils.DateUtil;
import com.boot2.core.utils.HttpsClient;
import com.boot2.core.utils.ServletUtil;
import com.boot2.core.utils.XSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhangweilin
 * @Description: xxxxx
 * @date 2020/5/30
 */
@Component
@Slf4j
public class RestartedSuccessHandler implements SmartLifecycleHandler {

    @Autowired
    Environment environment;

    @Autowired
    SysLogService sysLogService;

    /**
     * 外网ip
     */
    private String extranetIP;

    /**
     * 内网ip
     */
    private String intranetIP;

    /**
     * profiles
     */
    private String[] activeProfiles;

    private static final String reg = "<div class=\"well\"><p>您现在的 IP.*</p></div>";

    @Override
    @Test
    public void hand() {
		new Thread(this::printStartInfo).start();
		String appName = environment.getProperty("spring.application.name");
		log.info("【{}】系统启动成功",appName);
    }

	private void printStartInfo() {
		try {
			Map<String, String> headerMap = new HashMap<>();
			headerMap.put("User-Agent", "Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11");
			String content = HttpsClient.getWithHeader("https://www.ip.cn/", headerMap, Collections.emptyMap());
			Pattern pattern = Pattern.compile(reg);
			//        Matcher matcher = pattern.matcher("mmmmmmmmm<title>您的IP地址是：120.204.161.103</title>ppppppppppp");
			Matcher matcher = pattern.matcher(content);
			if (matcher.find()) {
				extranetIP = matcher.group(0);
				extranetIP = XSSUtil.xssEncode(extranetIP);
			}

			intranetIP = ServletUtil.getIpAddr();
			activeProfiles = environment.getActiveProfiles();
			String appName = environment.getProperty("spring.application.name");

			Page<SysLog> sysLogPage = sysLogService.restartApp(appName);

			StringBuffer sb = new StringBuffer();
			sb.append("恭喜，只是告诉你，系统刚刚重启成功，不用紧张不用紧张\n\r");
			sb.append("\n\r首先，恭喜，这不是异常信息，而是告诉你，系统刚刚重启成功了，不用紧张不用紧张 ");
			sb.append("\n\r应用名称:【").append(appName).append("】");
			sb.append("\n\r环境:【").append(ArrayUtils.toString(activeProfiles)).append("】");
			sb.append("\n\r外网IP:【").append(extranetIP).append("】");
			sb.append("\n\r内网IP:【").append(intranetIP).append("】");
			sb.append("\n\r当前时间:").append(DateUtil.formatDate(new Date()));
			Long count = sysLogPage.getTotalElements();
			sb.append("\n\r本次是该应用第【").append(count).append("】次重启");
			sb.append("\n\r赶紧愉快的测试吧");
			sb.append("\n\r以下为最近的重启记录");
			if (null != sysLogPage) {
				List<SysLog> sysLogList = sysLogPage.getContent();
				for (int i = 0; i < sysLogList.size(); i++) {
					sb.append("\n\r第【").append(count-i).append("】次,时间:").append(DateUtil.formatDate(sysLogList.get(i).getCreateTime()));
				}
			}
			log.error(sb.toString());
		} catch (Exception e) {
			 log.error("重启成功打印信息失败，不影响业务",e);
		}
	}

}

